package com.clw.controller;

import com.clw.dataobject.Feature;
import com.clw.enums.ResultEnum;
import com.clw.exception.SellException;
import com.clw.form.FeatureForm;
import com.clw.service.FeatureService;
import com.clw.utils.ResultVOUtils;
import com.clw.utils.StringUtils;
import com.clw.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-12-15.
 */
@Controller
@RequestMapping("/feature")
public class FeatureController {
    @Autowired
    private FeatureService service;

    @RequestMapping("/treeView")
    public ModelAndView treeView(){
        return new ModelAndView("feature/index");
    }

    @RequestMapping("/getData")
    @ResponseBody
    public List<Feature> getData(HttpSession session){
        List<Feature> list=null;
        list=service.findAll();
        return list;
    }

    @RequestMapping("/view")
    public ModelAndView view(@RequestParam(value = "id",required = false) Integer id,
                             Map<String,Object> map){
        if (StringUtils.isNotEmpty(id)){
            Feature features= service.findOne(id);
            map.put("features",features);
        }
        return new ModelAndView("feature/add_view");
    }

    @PostMapping("/save")
    @ResponseBody
    public ResultVO save(@Valid FeatureForm form, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOUtils.error(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        Feature features=new Feature();
        try{
            if (StringUtils.isNotEmpty(form.getId())){
                features= service.findOne(form.getId());
                BeanUtils.copyProperties(form,features);
                service.save(features);
            }
            else {
                if (StringUtils.isEmpty(form.getPid()))
                {
                    form.setPid(0);
                }
                BeanUtils.copyProperties(form,features);
                service.add(features);
            }
        }catch (SellException e){
            return ResultVOUtils.error(ResultEnum.SAVE_ERROR.getCode(),
                    ResultEnum.SAVE_ERROR.getMessage());
        }
        return ResultVOUtils.success("保存成功");
    }

    @ResponseBody
    @PostMapping("del")
    public ResultVO del(@RequestParam(value = "id",required = false) Integer id){
        Feature features= service.findOne(id);
        if (features==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        Long childrenCount= service.getChildrenCount(features.getId());
        if (childrenCount>0){
            return ResultVOUtils.error(ResultEnum.HAVE_CHILDREN.getCode(),
                    ResultEnum.HAVE_CHILDREN.getMessage());
        }
        service.del(features);
        return ResultVOUtils.success("删除成功");
    }
}
